package com.hqyj.finance.server.users.financeusers.utils;

import cn.hutool.crypto.digest.BCrypt;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class MyUtils {

    private static String MySalt; //我自己的盐；


    //检查是否是合法的手机号码
    public static boolean isMobileNumber(String mobileStr) {
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4])|(18[0-9])|(17[0-9])|(19[0-9])|(147))\\d{8}$");
        Matcher m = p.matcher(mobileStr);
        return m.matches();
    }

    public static boolean isEmail(String email) {
        // return ReUtil.isMatch("^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+([-.][A-Za-z0-9]+)*\\.\\w+([-.][A-Za-z0-9]+)*$",email);
        //正则表达式匹配任何字符
        String regex = "^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$";
        //编译正则表达式
        Pattern pattern = Pattern.compile(regex);
        //检索匹配器对象
        Matcher m = pattern.matcher(email);
        return m.matches();
    }

    @Value("${myproject.bcrypt-salt}")
    public void setSalt(String salt){
        MyUtils.MySalt = salt;
    }

    public static String encodePassword(String password){
        return BCrypt.hashpw(password, BCrypt.gensalt());
    }

    public static boolean checkPassword(String password,String encodePass){
        return BCrypt.checkpw(password,encodePass);
    }
}
